﻿using CSharp.Study.Generic.NetAttributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using CSharp.Study.Generic.Classes;

namespace CSharp.Study.Generic
{
    public class 特性反射
    {
        public static void Go()
        {
            var attRef = new AttRef<int, string>()
            {
                Id = 1001,
                List = new List<(int, string)> { (1, "a"), (2, "b"), (3, "c") }
            };
            //反射
            Type type = attRef.GetType();
            //类型名
            string a1 = type.Name;
            //程序集
            Assembly aa = type.Assembly;
            //命名空间
            string a2 = type.Namespace;
            //获取全部泛型
            var sda = type.GetGenericArguments();
            //获取属性
            PropertyInfo[] ps = type.GetProperties();
            //获取字段
            FieldInfo[] fields = type.GetFields();
            //获取特性
            var attrs = type.GetCustomAttributes();
            var net = type.GetCustomAttribute(typeof(NetAttribute)) as NetAttribute;
            //获取方法，并调用方法
            MethodInfo[] methods = type.GetMethods();
            MethodInfo show = type.GetMethod("Show");
            show.Invoke(attRef, null);
            MethodInfo showMsg = type.GetMethod("ShowMsg");
            showMsg.Invoke(attRef,new object[] { "aaa" } );

            var aa1 = new Aaa1
            {
                Id = 1,
                Name = "km"
            };
            Test(aa1);
        }


        public static void Test<T>(T entity) where T : class
        {
            Type type = typeof(T);
            List<object?> objects = new List<object?>();
            foreach (var property in type.GetProperties())
            {
                objects.Add(property.GetValue(entity));
            }
        }

    }
}
